package boogui.movie;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import boogui.db.BooguiDAO;
import boogui.db.IBooguiDAO;
import boogui.db.MovieSQL;

public class MovieSelectDAO implements IMovieSelectDAO {
	IBooguiDAO bdao = null;
	MovieSQL mvsql = null;

	public MovieSelectDAO() {
		bdao = new BooguiDAO();
		mvsql = new MovieSQL();
	}

	@Override
	public List<MovieVO> movieTitle() {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<MovieVO> result = new ArrayList<>();
		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(mvsql.getTitleSQL());
			rs = pstmt.executeQuery();
			while (rs.next()) {
				MovieVO m = new MovieVO();
				m.setMovtitle(rs.getString("title"));
				result.add(m);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, rs);
		}

		return result;
	}

	@Override
	/*
	 * private String locationSQL = "select distinct location" +
	 * " from showing s inner join movies m" +
	 * " on s.movno = m.movno inner join cinema c" +
	 * " on c.cineno = s.cineno where title = ?";
	 */
	public List<MovieVO> movieArea(MovieVO m) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<MovieVO> result = new ArrayList<>();

		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(mvsql.getLocationSQL());
			pstmt.setString(1, m.getMovtitle());

			rs = pstmt.executeQuery();

			while (rs.next()) {
				MovieVO mv = new MovieVO();
				mv.setLocation(rs.getString("location"));
				result.add(mv);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, rs);
		}
		return result;
	}

	@Override
	public List<MovieVO> movieCinema(MovieVO m) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<MovieVO> result = new ArrayList<>();
		
		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(mvsql.getCinemaSQL());
			pstmt.setString(1, m.getMovtitle());
			pstmt.setString(2, m.getLocation());
			rs = pstmt.executeQuery();

			while (rs.next()) {
				MovieVO mv = new MovieVO();
				mv.setCinename(rs.getString("cinename"));
				mv.setTheater(rs.getString("theater"));
				result.add(mv);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, rs);
		}
		return result;
	}		
	
	@Override
	public List<MovieVO> movieDate(MovieVO m) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<MovieVO> result = new ArrayList<>();		
		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(mvsql.getDateSQL());
			pstmt.setString(1, m.getMovtitle());
			pstmt.setString(2, m.getLocation());
			pstmt.setString(3, m.getCinename());
			pstmt.setString(4, m.getTheater());
			rs = pstmt.executeQuery();
			while (rs.next()) {
				MovieVO mv = new MovieVO();
				mv.setSdate(rs.getString("sdate"));
				result.add(mv);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, rs);
		}
		return result;
	}

	public List<MovieVO> movieTime(MovieVO m) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<MovieVO> result = new ArrayList<>();		
		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(mvsql.getTimeSQL());
			pstmt.setString(1, m.getMovtitle());
			pstmt.setString(2, m.getLocation());
			pstmt.setString(3, m.getCinename());
			pstmt.setString(4, m.getTheater());
			pstmt.setString(5, m.getSdate());
			
			rs = pstmt.executeQuery();
			while (rs.next()) {
				MovieVO mv = new MovieVO();
				mv.setStime(rs.getString("stime"));
				//System.out.println(mv.getStime());
				result.add(mv);
			}			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, rs);
		}
		return result;
	}

}
